package cn.wolfcode.luowowo.website.web.controller;

import cn.wolfcode.luowowo.article.domain.ScenicReviews;
import cn.wolfcode.luowowo.article.query.ScenicReviewsQuery;
import cn.wolfcode.luowowo.article.service.IDestinationService;
import cn.wolfcode.luowowo.article.service.IScenicReviewsService;
import cn.wolfcode.luowowo.article.service.IScenicreviewsTagService;
import cn.wolfcode.luowowo.cache.service.IScenicCommentCacheService;
import cn.wolfcode.luowowo.comment.domain.ScenicComment;
import cn.wolfcode.luowowo.comment.service.IScenicCommentService;
import cn.wolfcode.luowowo.exception.LogicException;
import cn.wolfcode.luowowo.member.domain.UserInfo;
import cn.wolfcode.luowowo.query.QueryObject;
import cn.wolfcode.luowowo.ticket.domain.Scenic;
import cn.wolfcode.luowowo.util.AjaxResult;
import cn.wolfcode.luowowo.website.annotation.RequiredLogin;
import cn.wolfcode.luowowo.website.annotation.UserParam;
import cn.wolfcode.luowowo.website.util.UploadUtil;
import com.alibaba.dubbo.config.annotation.Reference;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import java.util.Date;
import java.util.List;

/**
 * 处理景点的接口
 */
@Controller
@RequestMapping("/scenic")
public class ScenicController {

    @Value("${file.path}")
    private String fileDir;

    @Reference
    private IScenicReviewsService scenicService;
    @Reference
    private IDestinationService destinationService;
    @Reference
    private IScenicCommentService scenicCommentService;
    @Reference
    private IScenicreviewsTagService scenicreviewsTagService;
    @Reference
    private IScenicCommentCacheService scenicCommentCacheService;

    //全部景点分页查询 异步请求分页
    @RequestMapping("/survey")
    public String survey(@ModelAttribute("qo") ScenicReviewsQuery qo, Model model) {
        //pageInfo
        PageInfo pageInfo = scenicService.queryForList(qo);
        model.addAttribute("pageInfo", pageInfo);

        return "scenic/listTpl";
    }

    @RequestMapping("/list")
    public String list(@ModelAttribute("qo") ScenicReviewsQuery qo, Model model, Long destId, @UserParam UserInfo user) {
        //共享当前登录用户
        if (user != null) {
            model.addAttribute("user", user);
        }
        //根据吐司的destId查询出目的地信息
        model.addAttribute("dest", destinationService.selectByDestScenic(destId));
        //TOP5
        model.addAttribute("top5", scenicService.listTop5());
        //热门TOP10
        model.addAttribute("hot10", scenicService.listHot10());
        //景点标签
        model.addAttribute("tags", scenicreviewsTagService.selectAll());
        //全部景点
        model.addAttribute("all", scenicService.selectByScenic(destId));
        return "scenic/list";
    }

    //景点查询 评论数据查询
    @RequestMapping("/detail")
    public String detail(Long id, Model model, @UserParam UserInfo user) {
        if (user != null) {
            model.addAttribute("user", user);
        }
        ScenicReviews scenic = scenicService.getById(id);
        //单一景点
        model.addAttribute("scenic", scenic);
        //该景点目的地信息
        model.addAttribute("dest", scenic.getDest());
        //内部景点查询
        model.addAttribute("childs", scenicService.selectSonById(id));
        //查询真实评论数 蜂蜂点评（共有x条真实评价）
        model.addAttribute("commentCount", scenicCommentService.getCommentCount(id));
        //2星以下差评 category=1
        model.addAttribute("negativeRatings", scenicCommentService.getCountByCategory(1, id));
        //3星中评 category=2
        model.addAttribute("average", scenicCommentService.getCountByCategory(2, id));
        //4/5星好评 category=3
        model.addAttribute("favorableComment", scenicCommentService.getCountByCategory(3, id));
       //TODO 收藏回显
        List<Long> userIds = scenicCommentCacheService.getFavorUserIdByscenicId(id);
        model.addAttribute("userIds", userIds);
        return "scenic/detail";
    }

    //评论添加
    @RequestMapping("commentAdd")
    @ResponseBody
    @RequiredLogin
    public AjaxResult scenicCommentAdd(Model model, ScenicComment scenicComment, Integer floork, @UserParam UserInfo userInfo) {
        if (userInfo == null) {
            throw new LogicException("请先登录");
        }
        AjaxResult result = new AjaxResult();
        try {
            //设置用户信息
            scenicComment.setCreateTime(new Date());
            scenicComment.setUserId(userInfo.getId());
            scenicComment.setLevel(userInfo.getLevel());
            scenicComment.setHeadUrl(userInfo.getHeadImgUrl());
            scenicComment.setCity(userInfo.getCity());
            scenicComment.setUsername(userInfo.getNickname());
            scenicCommentService.commentAdd(scenicComment);
        } catch (Exception e) {
            e.printStackTrace();
            result.mark("评论失败败了呀");
        }
        return result;
    }

    //评论的评论
    @RequiredLogin
    @RequestMapping("/saveComment")
    @ResponseBody
    public AjaxResult saveComment(ScenicComment comment, @UserParam UserInfo user) {
        if (user == null) {
            throw new LogicException("请先登录");
        }
        AjaxResult result = new AjaxResult();
        try {
            comment.setCreateTime(new Date());
            comment.setUserId(user.getId());
            comment.setLevel(user.getLevel());
            comment.setHeadUrl(user.getHeadImgUrl());
            comment.setUsername(user.getNickname());
            comment.setCity(user.getCity());
            scenicCommentService.commentAdd(comment);
        } catch (Exception e) {
            e.printStackTrace();
            result.mark("评论失败败了呀");
        }
        return result;
    }

    //图片上传
    @ResponseBody
    @RequestMapping("/coverImageUpload")
    public Object uploadImg(MultipartFile pic) {
        String uploadUrl = UploadUtil.upload(pic, fileDir);
        return "/" + uploadUrl;
    }

    //评论查看
    @RequestMapping("/getComment")
    public Object getComment(@ModelAttribute("qo") cn.wolfcode.luowowo.query.ScenicReviewsQuery qo, Model model, Long scenicId) {
        //设置用户信息
        Page<ScenicComment> page = scenicCommentService.getComment(qo, scenicId);
        model.addAttribute("page", page);
        return "scenic/commentTpl";
    }

    //收藏
    @RequiredLogin
    @RequestMapping("/collect")
    @ResponseBody
    public AjaxResult collect(@UserParam UserInfo user, Long scenicId) {
        AjaxResult result = new AjaxResult();
        boolean success = scenicCommentCacheService.collect(user.getId(), scenicId);
        //设置收藏结果
        result.setSuccess(success);
        return result;
    }

    //去过
    @RequiredLogin
    @RequestMapping("/havegoto")
    @ResponseBody
    public AjaxResult havegoto(@UserParam UserInfo user, Long scenicId) {
        AjaxResult result = new AjaxResult();
        boolean success = scenicCommentCacheService.havegoto(user.getId(), scenicId);
        //设置收藏结果
        result.setSuccess(success);
        return result;
    }


    //评论点赞
    @RequiredLogin
    @RequestMapping("/scenicThumbup")
    @ResponseBody
    public AjaxResult scenicThumbup(String id, @UserParam UserInfo user) {
        AjaxResult result = new AjaxResult();
        if (user == null) {
            result.setCode(102);
            result.setMsg("请登录后再试!");
        }
        Integer state = scenicCommentService.strategyThumbup(id, user.getId());

        return result.addData(state);
    }


    /**
     * 评论分类
     *
     * @param model
     * @return
     */
    @RequestMapping("/searchCategory")
    public String searchCategory(cn.wolfcode.luowowo.query.ScenicReviewsQuery qo, Model model) {
        Page<ScenicComment> page = scenicCommentService.getCommentBtCategory(qo);
        model.addAttribute("page", page);
        return "scenic/commentTpl";
    }

    //景点评论分页
    @RequestMapping("/pageSearch")
    public Object pageSearch(cn.wolfcode.luowowo.query.ScenicReviewsQuery qo, Model model) {
        //设置用户信息
        Page<ScenicComment> page = scenicCommentService.getCommentBtCategory(qo);
        model.addAttribute("page", page);
        return "scenic/commentTpl";
    }

}
